{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Numpy数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3.]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1.0,2.0,3.0])\n",
    "print(x)\n",
    "\n",
    "y = np.array([[1,2,3],[4,5,6]])\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数组的运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基本运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 7 5]\n",
      "[ 0 -3  1]\n",
      "[ 1 10  6]\n",
      "[1.  0.4 1.5]\n",
      "[ 1 32  9]\n",
      "[1 0 1]\n",
      "***\n",
      "[[0.12257171 0.05733633 0.91392146]\n",
      " [0.08815538 0.01277902 0.03307354]]\n",
      "[[0.75504097 0.40680849 0.48024523]\n",
      " [0.20860756 0.99483737 0.147855  ]]\n",
      "[[0.87761268 0.46414482 1.3941667 ]\n",
      " [0.29676294 1.00761639 0.18092855]]\n",
      "[[-0.63246926 -0.34947217  0.43367623]\n",
      " [-0.12045218 -0.98205836 -0.11478146]]\n",
      "[[0.09254666 0.0233249  0.43890643]\n",
      " [0.01838988 0.01271304 0.00489009]]\n",
      "[[0.16233783 0.14094181 1.90303079]\n",
      " [0.42258957 0.01284533 0.22368905]]\n",
      "[[0.2049733  0.31254944 0.9576938 ]\n",
      " [0.60251925 0.01306992 0.60408468]]\n",
      "[[0. 0. 1.]\n",
      " [0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1,2,3])\n",
    "b = np.array([1,5,2])\n",
    "\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)\n",
    "print(a**b)\n",
    "print(a//b)\n",
    "\n",
    "print(\"***\")\n",
    "a = np.random.rand(2,3)\n",
    "b = np.random.rand(2,3)\n",
    "print(a)\n",
    "print(b)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)\n",
    "print(a**b)\n",
    "print(a//b)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 广播（Broadcast）\n",
    "广播遵循规则：\n",
    "1. 由右至左逐位比较；\n",
    "2. 当双方维数相同或者有一方为1时，继续比较；否则返回异常；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "[[3 4 5]\n",
      " [4 5 6]]\n",
      "[[4 6 8]\n",
      " [5 7 9]]\n",
      "[20 30 40]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1,2,3]) # 1*3\n",
    "b = np.array([[3,4,5],[4,5,6]]) # 2*3\n",
    "\n",
    "print(a)\n",
    "print(b)\n",
    "print(a+b)\n",
    "\n",
    "a = np.array([10,20,30]) # 1*3\n",
    "b=10 # 1\n",
    "print(a+b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 索引与切片"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基础索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5]\n",
      "0\n",
      "1\n",
      "5\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "a=np.array([0,1,2,3,4,5])\n",
    "print(a)\n",
    "\n",
    "print(a[0])\n",
    "print(a[1])\n",
    "print(a[a.size-1])\n",
    "\n",
    "print(a[-2])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "[0 1 2 3]\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[0,1,2,3],[4,5,6,7],[8,9,10,11]])\n",
    "print(a)\n",
    "\n",
    "print(a[0])\n",
    "print(a[0][-2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 切片索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5]\n",
      "[0 1 2 3 4]\n",
      "[0 1 2 3 4 5]\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "[[0 1 2 3]\n",
      " [4 5 6 7]]\n",
      "[0 4 8]\n"
     ]
    }
   ],
   "source": [
    "a=np.array([0,1,2,3,4,5])\n",
    "print(a)\n",
    "print(a[0:-1])\n",
    "print(a)\n",
    "\n",
    "a = np.array([[0,1,2,3],[4,5,6,7],[8,9,10,11]])\n",
    "print(a)\n",
    "print(a[:2])\n",
    "print(a[:,0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 布尔索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3]\n",
      "[True, True, True, True]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=np.array([0,1,2,3])\n",
    "print(a)\n",
    "idx = []\n",
    "for i in np.arange(4):\n",
    "    rnd = np.random.rand(1)\n",
    "    if rnd>0.5:\n",
    "        idx.append(True)\n",
    "    else:\n",
    "        idx.append(False)\n",
    "print(idx)\n",
    "a[idx]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False False  True  True  True]\n",
      "[0 1 2]\n",
      "(array([2, 3, 4], dtype=int64),)\n",
      "[0 1 2]\n"
     ]
    }
   ],
   "source": [
    "a=np.array([-2,-1,0,1,2])\n",
    "print(a>=0)\n",
    "print(a[a>=0])\n",
    "\n",
    "print(np.where(a>=0))\n",
    "b = np.array([i for i in a if i>=0])\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 循环迭代访问"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "****\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n"
     ]
    }
   ],
   "source": [
    "a=np.array([0,1,2,3,4,5])\n",
    "for i in a:\n",
    "    print(i)\n",
    "\n",
    "a = np.array([[0,1,2,3],[4,5,6,7],[8,9,10,11]])\n",
    "for i in a:\n",
    "    for j in i:\n",
    "        print(j)\n",
    "\n",
    "print(\"****\")\n",
    "for i in np.nditer(a): # 按照内存中的存储位置来索引\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 花式索引（神奇索引）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0. 0.]\n",
      " [1. 1. 1. 1.]\n",
      " [2. 2. 2. 2.]\n",
      " [3. 3. 3. 3.]\n",
      " [4. 4. 4. 4.]\n",
      " [5. 5. 5. 5.]\n",
      " [6. 6. 6. 6.]\n",
      " [7. 7. 7. 7.]]\n"
     ]
    }
   ],
   "source": [
    "a = np.empty((8, 4))\n",
    "for i in np.arange(8):\n",
    "    a[i] = i # 广播: 赋值运算符也会发生广播\n",
    "print(a)\n",
    "\n",
    "idx=[0,1,2]\n",
    "# print(a[idx])\n",
    "\n",
    "idx=[0,0,-1]\n",
    "# print(a[idx])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]\n",
      " [24 25 26 27]\n",
      " [28 29 30 31]]\n",
      "[ 0  5 10]\n",
      "[[ 5  7]\n",
      " [13 15]]\n",
      "[  6  22  38  54  70  86 102 118]\n",
      "[112 120 128 136]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\DELL\\AppData\\Local\\Temp\\ipykernel_10940\\1788593217.py:9: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.\n",
      "  print(a[idx])\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(32).reshape(8, 4)\n",
    "print(a)\n",
    "\n",
    "idx = [\n",
    "    [0,1,2],\n",
    "    [0,1,2]\n",
    "]\n",
    "\n",
    "print(a[idx])\n",
    "print(a[[1, 3]][:,[1,3]])\n",
    "\n",
    "a[[1, 3]][:,[1,3]]\n",
    "\n",
    "print(np.sum(a,axis=1))\n",
    "print(a.sum(0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 常用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2]\n",
      " [3 4 5]]\n",
      "15\n",
      "[3 5 7]\n",
      "[ 3 12]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(6).reshape(2, 3)\n",
    "print(a)\n",
    "print(np.sum(a))\n",
    "\n",
    "print(np.sum(a, axis=0))\n",
    "print(np.sum(a, axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.68621561  0.54794393 -0.03638254 -0.84701625  1.90230404]\n",
      " [ 0.27960539  0.6202545  -1.06856771 -0.72262073  0.08413956]\n",
      " [-0.58445516  0.60202227  0.43836524 -0.78234319  0.19293598]\n",
      " [ 0.00402491 -0.16407529 -1.14881226 -0.8355085   0.21045073]\n",
      " [ 1.01398463 -0.97019782  1.21718156  0.18264738 -1.26981959]\n",
      " [ 0.32338993  0.88577486  0.26439469  2.31912732  0.30866767]\n",
      " [ 0.89256403  0.0110227   0.98007413 -2.39557201 -0.85752252]\n",
      " [-0.36427809  0.5039269   0.18833121  1.08522707  0.35693907]\n",
      " [ 0.20732957 -0.14506454  0.16390425  0.82951248  0.75529996]\n",
      " [ 0.87948569 -0.2399563  -0.69505665 -0.47140008  0.34947641]]\n",
      "50\n",
      "50\n",
      "(10, 5)\n",
      "(2, 25)\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "a = np.random.randn(10, 5)\n",
    "print(a)\n",
    "\n",
    "print(a.size)\n",
    "b = a.flatten()\n",
    "print(b.size)\n",
    "\n",
    "print(a.shape)\n",
    "\n",
    "c = a.reshape(2, 25)\n",
    "print(c.shape)\n",
    "\n",
    "print(a.ndim)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "9775b82d8eac0bdc77741adceff29c45cb68e9290acb590ea3353a5619486182"
  },
  "kernelspec": {
   "display_name": "Python 3.8.12 ('anodet_env')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
